/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLayer;

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.AlgorithmParameterSpec;
import java.sql.Blob;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author seiterl
 */
public class DRM_Packager {
    
    private DRM_Packager() {}

    public static byte[] encryptGameWithSymKey(Blob game, Blob symKey) throws InvalidAlgorithmParameterException {
//        System.out.println("start");
//        File toencfilename = new File("C:/box/girl.jpg");

//        KeyGenerator kg = KeyGenerator.getInstance("DES");
//        kg.init(56);
//        SecretKey sk = kg.generateKey();

//        byte[] iv_bytes = sk.getEncoded();

//        try {
//            String FilePath = new String("C:/box/key");
//            FileOutputStream fos = new FileOutputStream(FilePath);
//            fos.write(sk.getEncoded());
//            fos.close();
//        } catch (Exception e) {
//        }
        byte[] toenc_byte = null;
        byte[] key_bytes = null;
        byte[] cipherText = null;

        byte[] iv = new byte[]
        {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
        };

        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
        
        toenc_byte = Helper.blob2byte(game);
        key_bytes = Helper.blob2byte(symKey);
        
        SecretKeySpec k = new SecretKeySpec(key_bytes, "AES");
        try {
            // = new byte[toenc_byte.length];
            Cipher cipher;
            
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, k,paramSpec);
            //int ctLength = cipher.update(toenc_byte, 0, toenc_byte.length, cipherText, 0);
            //ctLength += cipher.doFinal(cipherText, ctLength);
            cipherText = cipher.doFinal(toenc_byte);
            return cipherText;
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(DRM_Packager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(DRM_Packager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(DRM_Packager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(DRM_Packager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(DRM_Packager.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
//        try {
//            String FilePath = new String("C:/box/encrypted");
//            FileOutputStream fos = new FileOutputStream(FilePath);
//            fos.write(cipherText);
//            fos.close();
//        } catch (Exception e) {
//        }
    }

    public static byte[] encryptSymKeywithPublicKey(byte[] symKey, byte[] publicKey) {

        try {
            InputStream inStream = new ByteInputStream(publicKey, publicKey.length);
            CertificateFactory cf;
            cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);

            Cipher cipher;
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, cert.getPublicKey());
            byte[] cipherData;
            cipherData = cipher.doFinal(symKey);
            return cipherData;

        } catch (CertificateException ex) {
            System.out.println("DRM_Packager@encryptSymKeywithPublicKey : " + ex.getMessage());
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("DRM_Packager@encryptSymKeywithPublicKey : " + ex.getMessage());
        } catch (NoSuchPaddingException ex) {
            System.out.println("DRM_Packager@encryptSymKeywithPublicKey : " + ex.getMessage());
        } catch (InvalidKeyException ex) {
            System.out.println("DRM_Packager@encryptSymKeywithPublicKey : " + ex.getMessage());
        } catch (IllegalBlockSizeException ex) {
            System.out.println("DRM_Packager@encryptSymKeywithPublicKey : " + ex.getMessage());
        } catch (BadPaddingException ex) {
            System.out.println("DRM_Packager@encryptSymKeywithPublicKey : " + ex.getMessage());
        }

        return null;
        // inStream.close();

        //File topublicenc = new File("C:/asym/key");

        //byte[] topubenc_byte = null;

        //topubenc_byte = Kryptoasym1.getBytesFromFile(topublicenc);

        //System.out.println(cert.getPublicKey());

        //Key pubKey = cert.getPublicKey();

//        try {
//            String FilePath = new String("C:/asym/pubencrypted");
//            FileOutputStream fos = new FileOutputStream(FilePath);
//            fos.write(cipherData);
//            fos.close();
//        } catch (Exception e) {
//        }
    }
}
